open Printf
open Common
open ExtLib

let create_ok_cancel_dilaog ?ok ?cancel () =
	let d = GWindow.dialog ~modal:true () in
	let run f =
		f ()
	in
	let response_func = function
		| `OK ->
				Option.may run ok
		| `CANCEL | `DELETE_EVENT ->
				Option.may run cancel
		| _ -> ()
	in
		d#add_button_stock `OK `OK;
		d#add_button_stock `CANCEL `CANCEL;
		d#set_default_response `CANCEL;
		d#connect#response ~callback:response_func;
		d

let setup_top_window wnd =
	wnd |> ignore

let execute_top_window window =
	ignore (window#connect#destroy ~callback:GMain.Main.quit);
	window#show ();
	GMain.Main.main ()

let query_film () =
	let vb = GPack.vbox () in
	let fBox = GPack.vbox ~packing:vb#pack () in
	let sBox = GPack.vbox ~packing:(vb#pack ~expand:true ~fill:true) () in
		ignore(GMisc.label ~text:"Enter movie's title:" ~packing:(fBox#pack ~from:`START) ());
		ignore(GMisc.label  ~text:"Enter movie's description:" ~packing:(sBox#pack ~from:`START) ());
		let sc_win = GBin.scrolled_window
			~shadow_type:`NONE
			~hpolicy:`NEVER
			~vpolicy:`AUTOMATIC
			~packing:(sBox#pack ~expand:true ~fill:true)()
		in 

		let titleEdit = GEdit.entry ~packing:fBox#pack () in
		let descrEdit = GText.view ~buffer:(GText.buffer()) ~packing:sc_win#add ~editable:true () in
			
		let d = create_ok_cancel_dilaog
			~ok:(fun() ->
						 let t = titleEdit#text
						 and d = descrEdit#buffer#get_text() in
							 printf "Title: %s, Description: %s" t d
					)
			()
		in
			d#vbox#add vb#coerce;
			match d#run () with
				| `OK ->
						Some( titleEdit#text, descrEdit#buffer#get_text() )
				| _ ->
						None
				
let _ =
	match query_film () with
		| Some(title, description) ->
				let out = open_out "test.txt" in
					output_string out (sprintf "Title: %s\n" title);
					output_string out (sprintf "Description: %s\n" description);
					flush out;
					close_out out;
		| None -> ()
